//
//  MynodeListViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/11/2.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import SwiftyUserDefaults
import MJRefresh

class MynodeListViewController: BaseViewController ,UITableViewDataSource, UITableViewDelegate{
    // 顶部刷新
    var teamType : NSString?
    var isFrom :String!
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    var num = Int()
    var tempArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
        num = 1
        makeBaseUI()
        getData()
        // Do any additional setup after loading the view.
    }
    //MARK: - Private Methods
    func layoutNav(){
        //nav
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.title = XALanguageManager.getString(key: "节点列表")
    }
    
    
    
    func getData() {
        MyNetwork.request(target: .getWaydetail(user_id: Defaults[.userID]!, page: String(format:"%d",num), limit: "10", type: teamType! as String), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            self.footer.endRefreshing()
            self.header.endRefreshing()
            if needModel.code == 1111{
                var tempArr1 = NSMutableArray.yy_modelArray(with: productModel.self, json: needModel.mydata!["data"]!)
                for tempModel in tempArr1!{
                    self.tempArr.add(tempModel)
                }
                if (tempArr1?.count)! < 10{
                    self.footer.endRefreshingWithNoMoreData()
                }
                
                self.myTableView.reloadData()
                
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in self.navigationController?.view.makeToast(error.localizedDescription)
        }
        
    }
    
    
    func makeBaseUI() {
        //ui
        // 下拉刷新
        self.view.addSubview(myTableView)
        header.setRefreshingTarget(self, refreshingAction: #selector(RecordingListViewController.headerRefresh))
        myTableView.backgroundColor = UIColor.clear
        // 现在的版本要用mj_header
        /*
         header.setTitle("下拉刷新", for: .idle)
         header.setTitle("释放更新", for: .pulling)
         header.setTitle("正在刷新...", for: .refreshing)
         self.tableview.mj_header = header
         */
        
        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(RecordingListViewController.footerRefresh))
        self.myTableView.mj_footer = footer
        self.myTableView.mj_footer.isHidden = false
        self.myTableView.mj_header = header
        
        
        //        header.beginRefreshing()
    }
    
    // 顶部刷新
    @objc func headerRefresh(){
        print("下拉刷新")
        num = 1
        self.tempArr = NSMutableArray()
        getData()
    }
    
    // 底部刷新
    @objc func footerRefresh(){
        getData()
        num = 1 + num
        print("上拉刷新")
    }
    
    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:MyNodeListTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")as?MyNodeListTableViewCell
        if cell == nil {
            cell = MyNodeListTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        }
        cell.selectionStyle = .none
        cell.isFrom = self.isFrom
        cell.model = self.tempArr[indexPath.section] as! productModel
        cell.backgroundColor = UIColor.clear
        return cell!
    }
    //footer height
//    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return CGFloat.leastNormalMagnitude
//    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.tempArr.count
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
            var tempheight = CGFloat()
            tempheight = 64
            if #available(iOS 11.0.0, *) {
                tempheight = 0
                print("iOS 9.3.5 is available")
            }
        
        let myTableView = UITableView(frame: CGRect(x: 0, y: tempheight, width: kScreenWidth, height: kScreenHeight-tempheight), style: UITableViewStyle.grouped)
        myTableView.separatorStyle = .none
        
        myTableView.delegate = self
        myTableView.dataSource = self
        myTableView.register(MyNodeListTableViewCell.self, forCellReuseIdentifier: "cell")
        myTableView.register(UINib.init(nibName: "MyNodeListTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        return myTableView
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
